package com.kyivmohyla.kma2.client.presenter.refbook;

import com.google.gwt.event.dom.client.*;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;
import com.kyivmohyla.kma2.client.UserService;
import com.kyivmohyla.kma2.client.gin.Injector;
import com.kyivmohyla.kma2.client.presenter.Presenter;
import com.kyivmohyla.kma2.client.utils.Dialog;
import com.kyivmohyla.kma2.shared.model.UIEquipment;
import com.kyivmohyla.kma2.shared.model.UIService;

import java.util.ArrayList;
import java.util.List;

public class NewItemTreeGridViewPresenter implements Presenter {

    private final Display display;
    private final Injector injector = Injector.INSTANCE;
    private String type;
    private List<UIEquipment> uiEquipmentList;
    private List<UIService> uiServiceList;
    private int selectedItemIndex = 0;
    private Object existingItem;

    public NewItemTreeGridViewPresenter(String type) {
        this.display = injector.getNewItemTreeGridView();
        this.type = type;
        initCategoriesList(-1);
        bind();
    }

    public NewItemTreeGridViewPresenter(String type, Object object) {
        this.display = injector.getNewItemTreeGridView();
        this.type = type;
        this.existingItem = object;
        bind();
        if (type.equals("Equipment")) {
            display.getName().setText(((UIEquipment) object).getName());
            display.getDescription().setText(((UIEquipment) object).getDescription());
            initCategoriesList(((UIEquipment) object).getParentId());
        }
        if (type.equals("Service")) {
            display.getName().setText(((UIService) object).getName());
            display.getDescription().setText(((UIService) object).getDescription());
            initCategoriesList(((UIService) object).getParentId());
        }

    }

    private void bind() {
        this.display.getIsParent().addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent clickEvent) {
                boolean checked = ((CheckBox) clickEvent.getSource()).getValue();
                doChangeIsParent(checked);
            }
        });
        this.display.getCategory().addChangeHandler(new ChangeHandler() {
            @Override
            public void onChange(ChangeEvent changeEvent) {
                selectedItemIndex = display.getCategory().getSelectedIndex();
            }
        });
    }

    private void doChangeIsParent(Boolean checked) {
        if (checked) {
            this.display.getCategory().setEnabled(false);
            this.display.getCategory().setItemSelected(selectedItemIndex, true);
        } else {
            this.display.getCategory().setEnabled(true);
        }
    }

    private void initCategoriesList(final int parentId) {
        if (type.equals("Equipment")) {
            UserService.App.getInstance().getEquipmentAllEquipment(new AsyncCallback<List<UIEquipment>>() {
                @Override
                public void onFailure(Throwable throwable) {
                    Dialog.getInstance().createDialogBoxWithMessage("Error", throwable.getMessage());
                }

                @Override
                public void onSuccess(List<UIEquipment> uiEquipments) {
                    uiEquipmentList = new ArrayList<UIEquipment>(uiEquipments);
                    int selectedIndex = 0;
                    int count = 0;
                    List<String> parentObjects = new ArrayList<String>();
                    for (UIEquipment uiEquipment : uiEquipments) {
                        if (uiEquipment.getIsParent() == 1) {
                            parentObjects.add(uiEquipment.getName());
                            count++;
                            if (parentId != -1 && uiEquipment.getEquipmentId() == parentId) {
                                selectedIndex = count;
                            }
                        }
                    }
                    display.displayCategories(parentObjects);
                    display.getCategory().setItemSelected(selectedIndex, true);
                    selectedItemIndex = selectedIndex;
                }
            });
        } else if (type.equals("Service")) {
            UserService.App.getInstance().getServiceAllService(new AsyncCallback<List<UIService>>() {
                @Override
                public void onFailure(Throwable throwable) {
                    Dialog.getInstance().createDialogBoxWithMessage("Error", throwable.getMessage());
                }

                @Override
                public void onSuccess(List<UIService> uiServices) {
                    uiServiceList = new ArrayList<UIService>(uiServices);
                    int selectedIndex = 0;
                    int count = 0;
                    List<String> parentObjects = new ArrayList<String>();
                    for (UIService uiService : uiServices) {
                        if (uiService.getIsParent() == 1) {
                            parentObjects.add(uiService.getName());
                            count++;
                            if (parentId != -1 && uiService.getServiceId() == parentId) {
                                selectedIndex = count;
                            }
                        }
                    }
                    display.displayCategories(parentObjects);
                    display.getCategory().setItemSelected(selectedIndex, true);
                    selectedItemIndex = selectedIndex;
                }
            });
        }
    }

    @Override
    public void go(HasWidgets container) {
        container.clear();
        container.add(display.asWidget());
    }

    public Display getDisplay() {
        return this.display;
    }

    public List<UIEquipment> getUiEquipmentList() {
        return uiEquipmentList;
    }

    public List<UIService> getUiServiceList() {
        return uiServiceList;
    }

    public Object getExistingItem() {
        return existingItem;
    }

    public interface Display {

        HasClickHandlers getSaveButton();

        HasClickHandlers getCancelButton();

        HasText getName();

        HasText getDescription();

        CheckBox getIsParent();

        ListBox getCategory();

        void displayCategories(List<String> values);

        Widget asWidget();
    }
}
